package com.dream.keigezhushou.Activity.acty.play;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.JsonParse;
import com.dream.keigezhushou.Activity.Uiutils.PrefUtils;
import com.dream.keigezhushou.Activity.Uiutils.ToastUtils;
import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.acty.login.RegisterResponse;
import com.dream.keigezhushou.Activity.acty.play.fragment.EvaluateGameBean;
import com.dream.keigezhushou.Activity.url.NetURL;
import com.dream.keigezhushou.R;
import com.google.gson.Gson;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GameEvaluateActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.btn_publish)
    Button btnPublish;
    @BindView(R.id.et_content)
    EditText etContent;
    private String articleId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_evaluate);
        ButterKnife.bind(this);

        articleId = this.getIntent().getStringExtra("articleId");

        initView();
        initData();
    }

    //初始化界面
    public void initView() {

        tvTitle.setText("发表评论");
    }

    //初始化数据
    public void initData() {

        ivReturn.setOnClickListener(this);
        btnPublish.setOnClickListener(this);
    }

    //点击事件的实现

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.iv_return:

                //返回
                finish();
                break;

            case R.id.btn_publish:

                publishData();
                break;
        }
    }


    //将数据上传到服务器

    public void publishData() {

        String content = etContent.getText().toString();
        String userId = PrefUtils.getString(GameEvaluateActivity.this,"userId",null);

        ToastUtils.showToast(GameEvaluateActivity.this,userId);


        OkHttpUtils.postString()
                .url(NetURL.EvaluateGame)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(new Gson().toJson(new EvaluateGameBean(userId,articleId,content)))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {

                        RegisterResponse message = JsonParse.getFromJson(response,RegisterResponse.class);

                        String strMessage = message.getMessage();
                        String strState = message.getStatus();
                        int status = Integer.parseInt(strState);

                        if (status == 0){

                            ToastUtils.showToast(GameEvaluateActivity.this,strMessage + strState);

                            //当评论成功时，结束当前页面
                            finish();
                        }else {

                            ToastUtils.showToast(GameEvaluateActivity.this,strMessage);
                        }

                    }
                });
    }
}
